package oj;

import java.util.LinkedList;

public class LC394 {
    public String decodeString(String s) {
        int muli=0;
        StringBuilder res=new StringBuilder();
        LinkedList<Integer> sm=new LinkedList<>();
        LinkedList<String> sr=new LinkedList<>();

        for(Character c:s.toCharArray()){
            if(c=='['){
                sm.addLast(muli);
                sr.addLast(res.toString());
                res=new StringBuilder();
                muli=0;
            }else if(c==']'){
                StringBuilder tmp=new StringBuilder();
                int num=sm.removeLast();
                for(int i=0;i<num;i++){
                    tmp.append(res);
                }
                res=new StringBuilder(sr.removeLast()+tmp);
            }else if(c>='0' && c<='9'){
                muli=muli*10+Integer.parseInt(c+"");
            }else{
                res.append(c);
            }
        }
        return res.toString();
    }
}
